package com.zhenwei.zscm.repo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.ShippingExportViewEntity;
import com.zhenwei.zscm.mapper.IShippingExportViewMapper;
import com.zhenwei.zscm.mappings.ShippingExportViewMapping;
import com.zhenwei.zscm.model.dto.shipping.ShippingExportViewDTO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShippingExportViewRepo extends ServiceImpl<IShippingExportViewMapper, ShippingExportViewEntity> {

    public List<ShippingExportViewDTO> getAll(List<String> orderIds) {
        if (orderIds == null || orderIds.isEmpty()) {
            return new ArrayList<>();
        }
        if(orderIds.size() < 2000) {
            var q = new LambdaQueryWrapper<ShippingExportViewEntity>()
                    .in(ShippingExportViewEntity::getGuid, orderIds);
            List<ShippingExportViewEntity> list = this.list(q);
            return Linq.mapList(list, ShippingExportViewMapping.INSTANCE::entity2Dto);
        }
        int batchSize = 2000;
        var list = new ArrayList<ShippingExportViewDTO>();
        var pageNo = 1;
        while (true) {
            List<String> pageOrderIds = Linq.page(orderIds, pageNo, batchSize);
            if(Linq.isEmpty(pageOrderIds)) {
                break;
            }
            var q = new LambdaQueryWrapper<ShippingExportViewEntity>()
                    .in(ShippingExportViewEntity::getGuid, pageOrderIds);
            List<ShippingExportViewEntity> pageList = this.list(q);
            if(Linq.isEmpty(pageList)) {
                continue;
            }
            pageNo++;
            list.addAll(Linq.mapList(pageList, ShippingExportViewMapping.INSTANCE::entity2Dto));
        }
        return list;
    }
}
